//
//  ActivityXQViewController.m
//  个人项目(豆瓣元旦作业)
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 guoguo. All rights reserved.
//

#import "ActivityXQViewController.h"
#import "UIImageView+WebCache.h"

@interface ActivityXQViewController ()

@property (nonatomic, retain) UIScrollView *scrolView;
@property (nonatomic ,retain) NSArray *arr;
@property (nonatomic, retain) NSDictionary *dicData;

@end

@implementation ActivityXQViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    

    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = _mDic[@"title"];
    
    self.scrolView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _scrolView.backgroundColor = [UIColor whiteColor];
    
    _scrolView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height * 6);
    
    [self.view addSubview:_scrolView];
    
    
    [self handleData];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 300, 100)];
    
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:21];
    
    
    titleLabel.text = _dicData[@"title"];
    
    [_scrolView addSubview:titleLabel];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(18, 80, 120, 180)];
    
    imageView.backgroundColor = [UIColor redColor];
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:_mDic[@"image_hlarge"]]];
    
    
    [_scrolView addSubview:imageView];
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(139, 83, 20, 20)];
    
    UIImage *ima = [UIImage imageNamed:@"icon_date_blue@2x.png"];
    
    image1.image = ima;
    
    [_scrolView addSubview:image1];
    
    //时间拼接
    NSString *str1 = _mDic[@"begin_time"];
    NSString *str2 = _mDic[@"end_time"];
    NSString *str3 = [str1 substringWithRange:NSMakeRange(5, 11)];
    NSString *str4 = [str2 substringWithRange:NSMakeRange(5, 11)];
   
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(165, 81, 230, 25)];
    
    timeLabel.text = [str3 stringByAppendingFormat:@"--%@", str4];
    
    [_scrolView addSubview:timeLabel];
    
    
    UIImageView *image2 = [[UIImageView alloc] initWithFrame:CGRectMake(139, 110, 20, 20)];
    
    UIImage *ima1 = [UIImage imageNamed:@"icon_sponsor_blue@2x.png"];
    image2.image = ima1;
    
    [_scrolView addSubview:image2];
    
    UILabel *adLabel = [[UILabel alloc] initWithFrame:CGRectMake(165, 108, 230, 25)];
    NSDictionary *dic = _dicData[@"owner"];
    
    adLabel.text = dic[@"name"];
    
    [_scrolView addSubview:adLabel];
    
    UIImageView *image3 = [[UIImageView alloc] initWithFrame:CGRectMake(139, 140, 20, 20)];
    UIImage *ima3 = [UIImage imageNamed:@"icon_catalog_blue@2x.png"];
    image3.image = ima3;
    
    [_scrolView addSubview:image3];
    
    UILabel *typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(165, 140, 50, 20)];
    
    typeLabel.text = @"类型:";

    [_scrolView addSubview:typeLabel];
    
    
    UILabel *typeLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(210, 140, 230, 20)];
    
    typeLabel1.text = _mDic[@"category_name"];
    
    [_scrolView addSubview:typeLabel1];
    
    UIImageView *image4 = [[UIImageView alloc] initWithFrame:CGRectMake(139, 180, 20, 20)];
    UIImage *ima4 = [UIImage imageNamed: @"icon_spot_blue@2x.png"];
    image4.image = ima4;
    
    [_scrolView addSubview:image4];
    
    UILabel *addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(165, 170, 200, 80)];
    addressLabel.text = _mDic[@"address"];
    addressLabel.numberOfLines = 3;
    
    [_scrolView addSubview:addressLabel];
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 240, 200, 100)];
    label.text = @"活动介绍";
    label.font = [UIFont systemFontOfSize:23];
    [_scrolView addSubview:label];
    
   
   
    
    //顶端对齐问题
    UILabel *dateLabel = [[UILabel alloc] init];
  
    NSString *dateString = _mDic[@"content"];
    //默认为17号字体
    NSDictionary *art = @{NSFontAttributeName: [UIFont systemFontOfSize:17]};
    
    CGRect rect = [dateString boundingRectWithSize:CGSizeMake(340, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:art context:nil];
    
    
    
    [dateLabel setFrame:CGRectMake(20, 340, 340, rect.size.height)];
    
    dateLabel.text = dateString;
    dateLabel.numberOfLines = 2000;
    dateLabel.backgroundColor = [UIColor whiteColor];
    dateLabel.textAlignment = NSTextAlignmentLeft;

    
    [_scrolView addSubview:dateLabel];
    
    
    //添加收藏按钮
    UIImage *imageNav = [UIImage imageNamed:@"btn_nav_share@2x.png"];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:imageNav style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    
    self.navigationItem.rightBarButtonItem = item;
    
   
    self.str = @"hehe";
    
}

//实现barbuttonitem方法
- (void)itemAction:(UIBarButtonItem *)item{
    
    
    if ([_str isEqualToString:@"hehe"]) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
           _str = @"haha";
            
            
        }];
        
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction:action];
        [alert addAction:action1];
        
        
        [self presentViewController:alert animated:YES completion:nil];
    }
    
    if ([_str isEqualToString: @"haha"]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"该活动已被收藏过" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction:action];
        [self presentViewController:alert  animated:YES completion:^{
            
        }];
        
    }
    
    
    
    
}


//数据处理
- (void)handleData{
   
    NSString *pash = [[NSBundle mainBundle] pathForResource:@"activitylist" ofType:@"txt"];
    
    NSData *data = [NSData dataWithContentsOfFile:pash];
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    
    
    
   self.arr = dic[@"events"];
    
    for (NSDictionary *dic in _arr) {
        if ([dic[@"title"] isEqualToString:_mDic[@"title"]]) {
            self.dicData = [NSDictionary dictionaryWithDictionary:dic];
           
            NSLog(@"%@", _dicData);
            
        }
    }
    
    
    
    
    
    
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
